package com.android.jianboshi.ui.activity;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.ImageView;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.tool.Log;
import com.android.baselibrary.tool.SPUtil;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.jianboshi.R;
import com.android.jianboshi.bean.DataInfo;
import com.android.jianboshi.constants.SPConstants;
import com.android.jianboshi.dialog.Boxing_FreeStyleResult_DialogFragment;
import com.android.jianboshi.dialog.DaoJiShi_DialogFragment;
import com.android.jianboshi.event.Event_Notification;
import com.android.jianboshi.manager.BoxingHelper;
import com.android.jianboshi.manager.BoxingManager;
import com.android.jianboshi.ui.view.Tips_View;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

public class Boxing_FreeStyleActivity extends BaseActivity {

    @BindView(R.id.comm_title)
    Comm_HeadView comm_title;
    @BindView(R.id.iv_hit)
    ImageView iv_hit;
    @BindView(R.id.tips_view)
    Tips_View tips_view;

    BoxingManager boxingManager = new BoxingManager();
    Timer timer;
    int second = 0;   //进行了多少秒
    boolean isBegin = false;  //开始计数

    @Override
    protected int getLayoutId() {
        return R.layout.activity_boxing_freestyle;
    }

    @Override
    protected void initView() {
        comm_title.setTitle("");

        DaoJiShi_DialogFragment.showDialog(this, null).setCallback(new CommCallBack() {
            @Override
            public void onResult(Object obj) {
                //倒计时结束
                Log.i("倒计时结束");
                AnimationDrawable anim = (AnimationDrawable) iv_hit.getDrawable();
                anim.start();

                isBegin = true;

                timer = new Timer();
                timer.schedule(timerTask, 0, 1000);
            }
        });
    }

    @Override
    protected void initData() {
    }

    @Override
    protected boolean isRegistEventbus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event_Notification event) {
        if (isBegin) {
            Log.i("数据");
            boxingManager.parseScanResult(event.data);
            saveMax();  //每打一拳就存最大值
        }
        tips_view.setData(event.data);
    }

    @OnClick({R.id.tv_show_result, R.id.tv_show_record, R.id.tv_retry})
    public void onViewClicked(View view) {
        Map<String, Object> params = new HashMap<>();
        switch (view.getId()) {
            case R.id.tv_show_result:
                jisuanResult();
                break;
            case R.id.tv_show_record:
                maxResult();
                break;
            case R.id.tv_retry:   //再来一次
                startActivity(new Intent(mContext, Boxing_FreeStyleActivity.class));
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        saveMax();
    }

    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            second++;
        }
    };

    //查看当前成绩
    private void jisuanResult() {
        int hits = boxingManager.airPressureList.size();

        Map<String, Object> params = new HashMap<>();
        //时间
        params.put(Boxing_FreeStyleResult_DialogFragment.Param_Time, second);
        //击打次数
        params.put(Boxing_FreeStyleResult_DialogFragment.Param_Hits, hits);
        //计算平均力量
        List<Double> powerList = new ArrayList<>();
        for (DataInfo air : boxingManager.airPressureList) {
            double power = BoxingHelper.getPower(air);
            powerList.add(power);
        }
        double powerAvg = 0;
        if (powerList.size() > 0) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                powerAvg = powerList.stream().mapToDouble(Double::valueOf).average().getAsDouble();
            }
        }
        params.put(Boxing_FreeStyleResult_DialogFragment.Param_Avg_Power, powerAvg);
        //卡路里
        double kcal = BoxingHelper.getKCal(mContext, second, hits, powerAvg);
        params.put(Boxing_FreeStyleResult_DialogFragment.Param_Kcal, kcal);

        params.put("type", 0);   //查看当前成绩
        Boxing_FreeStyleResult_DialogFragment.showDialog(this, params);

    }

    //查看历史
    private void maxResult() {
        String Boxing_Free_Max_Time = SPUtil.getStringValue(mContext, SPConstants.Boxing_Free_Max_Time, "0");
        String Boxing_Free_Max_Hits = SPUtil.getStringValue(mContext, SPConstants.Boxing_Free_Max_Hits, "0");
        String Boxing_Free_Max_Power = SPUtil.getStringValue(mContext, SPConstants.Boxing_Free_Max_Power, "0");
        String Boxing_Free_Max_KCal = SPUtil.getStringValue(mContext, SPConstants.Boxing_Free_Max_KCal, "0");

        Map<String, Object> params = new HashMap<>();
        //时间
        params.put(Boxing_FreeStyleResult_DialogFragment.Param_Time, Integer.parseInt(Boxing_Free_Max_Time));
        //击打次数
        params.put(Boxing_FreeStyleResult_DialogFragment.Param_Hits, Integer.parseInt(Boxing_Free_Max_Hits));
        //计算平均力量
        params.put(Boxing_FreeStyleResult_DialogFragment.Param_Avg_Power, Double.parseDouble(Boxing_Free_Max_Power));
        //卡路里
        params.put(Boxing_FreeStyleResult_DialogFragment.Param_Kcal, Double.parseDouble(Boxing_Free_Max_KCal));
        params.put("type", 1);   //查看历史最大成绩
        Boxing_FreeStyleResult_DialogFragment.showDialog(this, params);

    }

    //保存最大成绩
    private void saveMax() {
        //击打次数
        int hits = boxingManager.airPressureList.size();

        //计算平均力量
        List<Double> powerList = new ArrayList<>();
        for (DataInfo air : boxingManager.airPressureList) {
            double power = BoxingHelper.getPower(air);
            powerList.add(power);
        }
        double powerAvg = 0;
        if (powerList.size() > 0) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                powerAvg = powerList.stream().mapToDouble(Double::valueOf).average().getAsDouble();
            }
        }
        //卡路里
        double kcal = BoxingHelper.getKCal(mContext, second, hits, powerAvg);


        int Boxing_Free_Max_Time = Integer.parseInt(SPUtil.getStringValue(mContext, SPConstants.Boxing_Free_Max_Time, "0"));
        int Boxing_Free_Max_Hits = Integer.parseInt(SPUtil.getStringValue(mContext, SPConstants.Boxing_Free_Max_Hits, "0"));
        double Boxing_Free_Max_Power = Double.parseDouble(SPUtil.getStringValue(mContext, SPConstants.Boxing_Free_Max_Power, "0"));
        double Boxing_Free_Max_KCal = Double.parseDouble(SPUtil.getStringValue(mContext, SPConstants.Boxing_Free_Max_KCal, "0"));

        if (second > Boxing_Free_Max_Time) {
            SPUtil.putValue(mContext, SPConstants.Boxing_Free_Max_Time, second + "");
        }
        if (hits > Boxing_Free_Max_Hits) {
            SPUtil.putValue(mContext, SPConstants.Boxing_Free_Max_Hits, hits + "");
        }
        if (powerAvg > Boxing_Free_Max_Power) {
            SPUtil.putValue(mContext, SPConstants.Boxing_Free_Max_Power, powerAvg + "");
        }
        if (kcal > Boxing_Free_Max_KCal) {
            SPUtil.putValue(mContext, SPConstants.Boxing_Free_Max_KCal, kcal + "");
        }
    }


}
